<?php

//
// IRC Parsing engine by Danopia
// For Ruffy v2.0
// Hehe, IRC parsing can be fun!
//
// The only varibles needed before including this file are $data, $comchar,
// and $nickname.
//

$args = explode(' ', $data); // Let's split into params

// See if the packet contains a message ( :message goes here)
$has_message = (strpos($data, ' :') !== null);
if ($has_message)
{ // yay message, let's parse it
	$message = trim(substr($data, strpos($data, ' :') + 2));
	$msg_args = explode(' ', $message); // exploding by space is useful

	// CTCP
	$is_ctcp = ((strlen($msg_args[0]) >= 2) &&
		(strpos($msg_args[0], chr(1)) === 0));
	if ($is_ctcp)
	{ // if it's a CTCP message, parse that as well
		$ctcp_msg = trim(substr($message, 1, strrpos($message, chr(1)) - 1));
		$ctcp_args = explode(' ', $ctcp_msg);
		$ctcp_command = strtoupper($ctcp_args[0]);
	} else { // take care of blanking the vars, so they aren't carried over
		$ctcp_command = '';
		$ctcp_args = array();
	}
	
	if (strpos($message, ' ') === false)
	{ // no params
		$msg_params = '';
	} else { // params == everything after the first word, in a string
		$msg_params = substr($message, strpos($message, ' ') + 1);
	}
	
} else {
 	$message = '';
 	$msg_params = '';
	$msg_args = array();
	$is_ctcp = false;
	$ctcp_msg = '';
	$ctcp_args = array();
	$ctcp_command = '';
}

// has point of origion, i.e. :nick!ident@host COMMAND :something.
$has_source = (strpos($args[0], ':') === 0);
if ($has_source) {
	$source = substr($args[0], 1);
	$args[0] = $source;
	$temp = explode('!', $args[0]);
	$has_source_path = count($temp) > 1;
	if ($has_source_path) { // has ident and hostname. servers do not have these
		$source_nick = $temp[0];
		$temp = explode('@', $temp[1]);
		$source_ident = $temp[0];
		$source_host = $temp[1];
	} else {
		$source_nick = $source;
		$source_ident = '';
		$source_host = '';
	}
	$command = strtoupper($args[1]);
} else {
	$command = strtoupper($args[0]);
	$source_nick = '';
	$source_ident = '';
	$source_host = '';
	$has_source_path = false;
}

// IRC Bot command parser.
$is_cmd = false;
$msg_cmd = ''; // lowercased command (first word))
$cmd_type = ''; // how the command was made (either pm, address, or comchar)
if ($has_message && ($command == 'PRIVMSG'))
{ // was a privmsg
	if ($args[2] == $nickname)
	{ // PM
		$is_cmd = true;
		$msg_cmd = strtolower($msg_args[0]);
		$args[2] = $source_nick;
		$cmd_type = 'pm';
	}
	else if((strlen($comchar) > 0) && (strpos($msg_args[0], $comchar) === 0))
	{ // comchar
		$msg_cmd = strtolower(substr($msg_args[0], strlen($comchar)));
		$is_cmd = true;
		$cmd_type = 'comchar';
	}
	else if ((stripos($msg_args[0], $nickname) === 0) && (((strlen($nickname) + 1)
		== (strlen($msg_args[0]))) || ($msg_args[0] == $nickname)))
	{ // address
		array_shift($msg_args);
		$msg_cmd = strtolower($msg_args[0]);
		$message = $msg_params;
		$msg_params = substr($message, strpos($message, ' ') + 1);
		$is_cmd = true;
		$cmd_type = 'address';
	}
	else if (in_array($nickname, $msg_args))
	{ // The bot was mentioned. Not used in convential commands, it's just there.
		$cmd_type = 'mention';
	}
}

// $msg_params2 is like $msg_params but with the first word cut off.
$msg_params2 = '';
if ($has_message && (strpos($msg_params, ' ') !== false))
{ // make sure ther'es another param!
	$msg_params2 = substr($msg_params, strpos($msg_params, ' ') + 1);
}

?>